package com.sskj.tibi.ui.activity;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.tibi.R;
import com.sskj.tibi.R2;
import com.sskj.tibi.bean.WithdrawRechargeCoinBean;
import com.sskj.tibi.bean.WithdrawRecordBean;
import com.sskj.tibi.presenter.WithdrawRecordActivityPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;


@Route(path = RConfig.TIBI_WITHDRAW_RECORD)//提币记录
public class WithdrawRecordActivity extends BaseActivity<WithdrawRecordActivityPresenter> {
    @BindView(R2.id.heySpinner)
    HeySpinner heySpinner;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    String code;
    private BottomSheetDialog bottomSheetCoin;
    private MVCCoolHelper<List<WithdrawRecordBean>> mvcHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.tibi_activity_withdraw_record;
    }

    @Override
    public WithdrawRecordActivityPresenter getPresenter() {
        return new WithdrawRecordActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity1));
        HashMap<String, String> tibiMap = new HashMap<>();

        tibiMap.put("1", App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity2));
        tibiMap.put("2", App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity3));
        tibiMap.put("3", App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity4));
        tibiMap.put("4", App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity5));
        tibiMap.put("5", App.INSTANCE.getString(R.string.tibi_withdrawRecordActivity6));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.tibi_recy_item_withdraw_record, new SlimInjector<WithdrawRecordBean>() {
            @Override
            public void onInject(WithdrawRecordBean data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvAddress, data.getQianbao_url())
                        .text(R.id.tvNum, data.getMoney())
                        .text(R.id.tvCreateTime, data.getCreated_at())
                        .text(R.id.tvCheckTime, data.getChecked_at())
                        .text(R.id.tvStatus, tibiMap.get(data.getStatus()));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<WithdrawRecordBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<WithdrawRecordBean>)
                page -> mPresenter.getRecord(page + "",code), 10);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    protected void initData() {
        mPresenter.getCoinType();
    }

    public void updateCoinList(List<WithdrawRechargeCoinBean> data) {
        heySpinner.setText("全部");
        Flowable.fromIterable(data)
                .map(withdrawRechargeCoinBean -> withdrawRechargeCoinBean.getCode())
                .toList()
                .subscribe((strings, throwable) -> {
                            strings.add(0,"全部");
                            ClickUtil.click(heySpinner, () -> {
                                bottomSheetCoin = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, i, view) -> {
                                    bottomSheetCoin.dismiss();
                                    if(i==0){
                                        code=null;
                                        heySpinner.setText("全部");
                                    }else {
                                        heySpinner.setText(data.get(i-1).getCode());
                                        code=data.get(i-1).getCode();
                                    }
                                    mvcHelper.refresh();

                                }, strings);
                                bottomSheetCoin.show();
                            });

                        }
                );
    }

}
